<?php
	/**
	 * Pagination Class
	 * 
	 * @package pagination
	 * @author Perkin Anton, Rostislav Rotaru
	 * @version 3.0
	 */
	class Pagination
	{		
		
		/* CONSTRUCTOR */
		
		/**
		 * Initializes a new instance of Pagination class
		 */
		public function __construct()
		{
			$this->IncludeFiles();
		}

		
		/* METHODS */
		
		/**
		 * Includes all necessary files
		 */
		private function IncludeFiles()
		{
			//Define separator constant
			(!defined('SEPARATOR')) ? define('SEPARATOR'.'/') : '';
			
			//Require necessary files files
			require_once(dirname(__FILE__).SEPARATOR.'paginationconfig.php');
			require_once(dirname(__FILE__).SEPARATOR.'paginationpool.php');
			require_once(dirname(__FILE__).SEPARATOR.'base'.SEPARATOR.'paginationbase.php');
			require_once(dirname(__FILE__).SEPARATOR.'base'.SEPARATOR.'paginationitembase.php');
		}
		
		/**
		 * Includes theme dependent files
		 * @param $theme pagination theme
		 */
		private function IncludeThemeFiles($theme)
		{
			//Include path
			$includesPath = str_replace($_SERVER['DOCUMENT_ROOT'], '', strtolower(str_replace('\\', '/', COMPONENTS.SEPARATOR.'pagination')));

			//Include CSS file
			if (file_exists(COMPONENTS.SEPARATOR.'pagination'.SEPARATOR.'themes'.SEPARATOR.$theme.SEPARATOR.$theme.'.css'))
			{
				echo '<link type = "text/css" rel = "stylesheet" href = "/./'.$includesPath.SEPARATOR.'themes'.SEPARATOR.$theme.SEPARATOR.$theme.'.css" />';
			}
		}		
		
		/**
		* Renders the pagination based on the given schema
		* 
		* @param array $schema pagination schema
		* 
		*/
		public function Render($schema)
		{
			//Check given schema default values
			$schema = PaginationConfig::ParseSchemaDefaults($schema);
			
			//includes theme dependent files
			$this->IncludeThemeFiles($schema['theme']);
			
			//Get the pagination renderer
			$paginationRenderer = PaginationConfig::CheckPool($schema['render_class'], 'pagination');
			
			//Renders (displays) pagination
			return $paginationRenderer->Render($schema);
		}
	}
?>